<?php
/**
 * Notification.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-19 15:54:50
 * @modified    2023-02-19 15:54:50
 *
 */

namespace App\Models;

use App\Repositories\NotificationRepo;
use Carbon\Carbon;

class Notification extends BaseModel
{
    protected $table = 'notification';

    protected $fillable = [
        'cid',
        'platform',
        'user_id',
        'title',
        'business_id',
        'business_type',
        'content',
        'comment',
        'create_user_id',
        'status'
    ];

    protected $appends = [
        'create_time_format',
        'update_time_format',
        'user_name', // 负责人
        'customer_name', // 客户
        'contact_name', // 联系人
        'contact_phone', // 联系电话
        'address', // 地址
        'plan_at', // 计划时间
        'event_number', // 事件编号
        'wo_number', // 工单编号
        'creator_name' // 创建人
    ];

    public function getCreateTimeFormatAttribute()
    {
        return Carbon::parse($this->create_time)->toDateTimeString();
    }

    public function getUpdateTimeFormatAttribute()
    {
        return Carbon::parse($this->update_time)->toDateTimeString();
    }

    public function getUserNameAttribute()
    {
        $type = $this->business_type;
        if ($type == NotificationRepo::TYPE_WORK_ORDER) {
            $user = UserModel::query()
                ->select(['user.realname'])
                ->join('notification as n', 'n.user_id', '=', 'user.id')
                ->where('n.id', $this->id)
                ->first();
            return $user->realname ?? '';
        }
        if ($type == NotificationRepo::TYPE_EVENT) {
            $user = \App\Models\Event\User::query()
                ->select(['user.username'])
                ->join('user', 'user.id', '=', 'event_user.user_id')
                ->where('event_user.event_id', $this->business_id)
                ->where('event_user.is_master', 1)
                ->first();
            return $user->realname ?? '';
        }
        return '';
    }

    public function getCustomerNameAttribute()
    {
        $type = $this->business_type;
        if ($type == NotificationRepo::TYPE_WORK_ORDER) {
            $customer = WorkOrder::query()
                ->select(['customer.name'])
                ->join('customer', 'customer.id', 'work_order.customer_id')
                ->where('work_order.id', $this->business_id)
                ->first();
            return $customer->name ?? '';
        }
        if ($type == NotificationRepo::TYPE_EVENT) {
            $customer = Event::query()
                ->select(['customer.name', 'event.customer_name'])
                ->leftJoin('customer', 'customer.id', 'event.customer_id')
                ->where('event.id', $this->business_id)
                ->first();
            return $customer->name ?? $customer->customer_name ?? '';
        }
        return '';
    }

    public function getContactNameAttribute()
    {
        $type = $this->business_type;
        if ($type == NotificationRepo::TYPE_WORK_ORDER) {
            $workOrder = WorkOrder::query()
                ->select(['work_order.contact_name'])
                ->where('work_order.id', $this->business_id)
                ->first();
            return $workOrder->contact_name ?? '';
        }
        if ($type == NotificationRepo::TYPE_EVENT) {
            $customer = Event::query()
                ->select(['event.contact_name'])
                ->where('event.id', $this->business_id)
                ->first();
            return $customer->name ?? '';
        }
        return '';
    }

    public function getContactPhoneAttribute()
    {
        $type = $this->business_type;
        if ($type == NotificationRepo::TYPE_WORK_ORDER) {
            $workOrder = WorkOrder::query()
                ->select(['work_order.contact_phone'])
                ->where('work_order.id', $this->business_id)
                ->first();
            return $workOrder->contact_phone ?? '';
        }
        if ($type == NotificationRepo::TYPE_EVENT) {
            $event = Event::query()
                ->select(['event.contact_telephone'])
                ->where('event.id', $this->business_id)
                ->first();
            return $event->contact_telephone ?? '';
        }
        return '';
    }

    public function getAddressAttribute()
    {
        $type = $this->business_type;
        if ($type == NotificationRepo::TYPE_WORK_ORDER) {
            $workOrder = WorkOrder::query()
                ->select(['work_order.address'])
                ->where('work_order.id', $this->business_id)
                ->first();
            return $workOrder->address ?? '';
        }
        if ($type == NotificationRepo::TYPE_EVENT) {
            $event = Event::query()
                ->select(['event.address'])
                ->where('event.id', $this->business_id)
                ->first();
            return $event->address ?? '';
        }
        return '';
    }

    public function getPlanAtAttribute()
    {
        $type = $this->business_type;
        if ($type == NotificationRepo::TYPE_WORK_ORDER) {
            $workOrder = WorkOrder::query()
                ->select(['work_order.plan_at'])
                ->where('work_order.id', $this->business_id)
                ->first();
            return $workOrder->plan_at ?? '';
        }
        if ($type == NotificationRepo::TYPE_EVENT) {
            return '';
        }
        return '';
    }

    public function getEventNumberAttribute()
    {
        $type = $this->business_type;
        if ($type != NotificationRepo::TYPE_EVENT) {
            return '';
        }
        $event = Event::find($this->business_id);
        return $event->event_number ?? '';
    }

    public function getWoNumberAttribute()
    {
        $type = $this->business_type;
        if ($type != NotificationRepo::TYPE_WORK_ORDER) {
            return '';
        }
        $workOrder = WorkOrder::find($this->business_id);
        return $workOrder->wo_number ?? '';
    }

    public function getCreatorNameAttribute()
    {
        $businessType = $this->business_type;
        $businessId = $this->business_id;
        if ($businessType == NotificationRepo::TYPE_EVENT) {
            $event = Event::find($businessId);
            return  $event->contact_name ?? '';
        } elseif ($businessType == NotificationRepo::TYPE_WORK_ORDER) {
            $workOrder = WorkOrder::find($businessId);
            return $workOrder->contact_name ?? '';
        }
        return '';
    }
}